package aeolus.wind.spider.web.controller;

import java.util.ArrayList;
import java.util.List;

import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;

import aeolus.wind.spider.biz.dataobject.Ability;
import aeolus.wind.spider.biz.dataobject.AbilityPackage;
import aeolus.wind.spider.web.commons.BaseController;
import aeolus.wind.spider.web.commons.ability.AbilityPackageItem;

@Controller
public class AbilityController extends BaseController {

    @RequestMapping(value = "manageAbility.htm")
    public String manageAbility(ModelMap model) {
        if (checkCanLeadGroup()) {
            List<AbilityPackage> abilityPackageList = abilityService.listAbilityPackages(getLoginName());
            List<AbilityPackageItem> abilityPackageItemList = new ArrayList<AbilityPackageItem>();
            if (abilityPackageList != null && abilityPackageList.size() > 0) {
                for (AbilityPackage abPackage : abilityPackageList) {
                    abilityPackageItemList.add(new AbilityPackageItem(abilityService, abPackage));
                }
            }
            model.addAttribute("abilityPackageItemList", abilityPackageItemList);
        }
        return getScreen("manageAbility");
    }

    @RequestMapping(value = "saveAbility.htm")
    public String saveAbility(String abilityName, String abilityDescription, String abilityPackageId, ModelMap model) {
        if (checkCanLeadGroup()) {
            if (StringUtils.isNotBlank(abilityName) && abilityPackageId != null) {
                AbilityPackage abPackage = abilityService.findAbilityPackage(abilityPackageId);
                if (abPackage != null) {// 原先的设计中package就算没有也可以创建能力，目前先让使用简单
                    Ability ability = new Ability(abilityName, abilityDescription, getLoginName());
                    abilityService.saveAbility(ability);
                    abPackage.addAbility(ability);
                    abilityService.saveAbilityPackage(abPackage);
                    model.addAttribute("showMessage", "创建能力" + ability.getName() + "成功");
                }
            }
        }
        return manageAbility(model);
    }

    @RequestMapping(value = "deleteAbility.htm")
    public String deleteAbility(String abilityId, String abilityName, String abilityPackageId, ModelMap model) {
        if (checkCanLeadGroup()) {
            if (abilityId != null && abilityPackageId != null) {
                AbilityPackage abPackage = abilityService.findAbilityPackage(abilityPackageId);
                if (abPackage != null) {// 原先的设计中package就算没有也可以创建能力，目前先让使用简单
                    abilityService.deleteAbility(abilityId);
                    abPackage.removeAbility(abilityId);
                    abilityService.saveAbilityPackage(abPackage);
                    model.addAttribute("showMessage", "删除能力" + abilityName + "成功");
                }
            }
        }
        return manageAbility(model);
    }
}
